package nancal.mp.db.mq


import nbcp.base.comm.const
import nbcp.base.extend.ToJson
import nbcp.base.utils.CodeUtil
import nbcp.base.utils.SpringUtil
import org.springframework.amqp.core.Message
import org.springframework.amqp.core.MessageProperties
import org.springframework.amqp.rabbit.connection.CorrelationData
import org.springframework.amqp.rabbit.core.RabbitTemplate

class MqSenderProxy<T>(val exchangeName: String = "") {

    //返回 MqLog.id
    fun send(entity: T): String {
        var json = entity.ToJson();
        var id = CodeUtil.getCode();
        var properties = MessageProperties();
        var message = Message(json.toByteArray(const.utf8), properties);

        mqTemplate.convertAndSend(exchangeName, exchangeName, message, CorrelationData(id));
        return id;
    }

    private val mqTemplate by lazy {
        return@lazy SpringUtil.getBean<RabbitTemplate>()
    }
}